package model;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;

import org.bushe.swing.event.EventBus;

import constants.Events;

/**
 * Turn Timer
 * @author Anton
 *
 */
public class TurnTimer {
	
	private static final int aSec = 1000;
	private Timer timer;
	private int startTime = 10;
	private int sec = startTime;
	
	/**
	 * Create a turn timer
	 * @param sec Seconds you want to start from
	 */
	public TurnTimer(){
		timer = new Timer(0, new ActionMan());
		timer.setDelay(aSec);
	} //constructor
	
	/**
	 * Set the start time
	 */
	public void setStartTime(int sec){
		this.sec = sec;
	}
	
	/**
	 * Get current start time
	 * @return current time in seconds
	 */
	public int getStartTime(){
		return startTime;
	} //getStartTime
	
	/**
	 * Starts the turn timer
	 */
	public void start(){
		timer.start();
	} //start
	
	public void stop(){
		timer.stop();
	} //stop
	
	public void restart(){
		timer.stop();
		sec = startTime;
		timer.restart();
	} //stop
	
	/**
	 * Inner class to perform a action when the time has ended
	 * @author Anton
	 *
	 */
	private class ActionMan implements ActionListener{
		
		@Override
		public void actionPerformed(ActionEvent arg0) { 
			timer.setDelay(1000);
			if(sec == -1)
				sec = 10;

			EventBus.publish(Events.TIME.toString(), sec);
			if(sec == 0){
				timer.setDelay(1500);
			}
			sec --;
			

		} //actionPerformed	
	}//ActionMan

} //end of class